//index.js
//获取应用实例
const app = getApp()
const Pager = require('components/aotoo/core')
let lib = Pager.lib
const defaultAdapter = require('../../utils/default')

const data1 = [
  { img: 'https://hbimg.huabanimg.com/3e4a71338e50cec6b9d11f11dcff45a70d93193e149ef-or8SFl_fw658/format/webp', title: '1新番动漫-没什么大不了死不了就还好'},
  { img: 'https://hbimg.huabanimg.com/0e7ec39e6483e3408b8dc7678a4ce1d108c3e07f1d79c-38WCrb_fw658/format/webp', title: '2新番动漫'},
  { img: 'https://hbimg.huabanimg.com/d603f483eb3be0343ae6079dd7c61339733595481099f-iXYPdJ_fw658/format/webp', title: '3新番动漫'},
  { img: 'https://hbimg.huabanimg.com/3e4a71338e50cec6b9d11f11dcff45a70d93193e149ef-or8SFl_fw658/format/webp', title: '4新番动漫'},
  { img: 'https://hbimg.huabanimg.com/0e7ec39e6483e3408b8dc7678a4ce1d108c3e07f1d79c-38WCrb_fw658/format/webp', title: '5新番动漫'},
  { img: 'https://hbimg.huabanimg.com/d603f483eb3be0343ae6079dd7c61339733595481099f-iXYPdJ_fw658/format/webp', title: '6新番动漫'},
  { img: 'https://hbimg.huabanimg.com/3e4a71338e50cec6b9d11f11dcff45a70d93193e149ef-or8SFl_fw658/format/webp', title: '7新番动漫'},
  { img: 'https://hbimg.huabanimg.com/0e7ec39e6483e3408b8dc7678a4ce1d108c3e07f1d79c-38WCrb_fw658/format/webp', title: '8新番动漫'},
  { img: 'https://hbimg.huabanimg.com/0e7ec39e6483e3408b8dc7678a4ce1d108c3e07f1d79c-38WCrb_fw658/format/webp', title: '9新番动漫'},
  { img: 'https://hbimg.huabanimg.com/d603f483eb3be0343ae6079dd7c61339733595481099f-iXYPdJ_fw658/format/webp', title: '10新番动漫'}
]
const adapterData = (data) => {
  return data.map(item => {
    return {
      img: item.img,
      title: item.title,
      id: lib.suid('wf_')
    }
  })
}

const adpater = (data, skeleton) => {
  let skeletonImgHei = [150, 180, 120, 200]
  return data.map((item, ii) => {
    if (skeleton) {
      return {
        title: {
          title: ' ',
          itemClass: 'item-pic',
          itemStyle: 'height: '+ skeletonImgHei[Math.floor(Math.random() * skeletonImgHei.length)] +'px'
        },
        dot: [
          { title: ' ', itemClass: 'item-title mb10'},
          { title: ' ', itemClass: 'item-title'},
        ],
        itemStyle: '--vsb: visible',
        itemClass: 'skeleton'
      }
    }
    else {
      return {
        id: item.id,
        img: {
          src: item.img,
          'lazy-load': true,
          itemClass: 'item-pic'
        },
        dot: [
          { title: item.id+'_'+item.title, itemClass: 'item-title size-form no-lh-x'},
          { title: ' ', itemClass: 'icon-before-like color-primary icon-absolute'}
        ],
        aim: 'onClick',
        catchlongpress: 'onDel?idx='+ii+'&id='+item.id,
        itemStyle: '--vsb: visible'
      }
    }
  })
}

Pager({
  data: {
    waterOther: true,
    waterfallLeft: Pager.list({
      $$id: 'left',
      data: adpater(data1, true),
      itemClass: 'demo-list-item ss-relative'
    }),
    waterfallRight: Pager.list({
      $$id: 'right',
      data: adpater(data1, true),
      itemClass: 'demo-list-item ss-relative'
    }),
    waterfallNodata: {
      $$id: 'other'
    }
  },
  newData: [], 
  idx: 0,
  onDel(e, params, inst) {
    const _idx = params.idx
    const _id = params.id
    // console.log(_idx % 2 == 0);
    // if (_idx % 2 == 0){
    //   //第一种，通过list组件提供的删除，在不改变原来的状态下，删除对应的数据
    //   this.$left.delete({id: _id})
    // }
    // else {
      // this.$right.delete({id: _id})
      //第二种，删除原始数据，再重新渲染
      console.log('=====id', _id);
      
      this.data = this.isOddEvent(adpater(this.dataInit).filter(item => item.id !== _id))
      this.updateData()
    // }
    wx.showToast({title: "已取消收藏"})
  },
  onReady() {
    this.$left = this.getElementsById('left')
    this.$right = this.getElementsById('right')
    this.$other = this.getElementsById('other')
    this.setData({
      'waterfallLeft.data': [],
      'waterfallRight.data': []
    })
    this.$other.update(defaultAdapter({title: '正在加载数据...', image: '/images/cat.png', itemClass: 'ss-fixed-r wid-p100'}))
    setTimeout(() => {
      this.updateData()
      this.$other.reset({title: '玩命加载中，请稍等...', itemClass: 'waterfall-load-tips'})
    }, 500);
  },
  isOddEvent(data) {
    let left = []
    let right = []
    for (var j = 0; j < data.length; j++) {   //--循环所有的列
      if (j%2 == 0){
        left.push(data[j])
      } else {
        right.push(data[j])
      }
    }
    return {left, right}
  },
  scrollTopStatus: true,
  onReachBottom() {
    this.scrollTopStatus = false
    const scrollTop = this.scrollTop
    
    const that = this
    if (this.idx < 5) {
      this.idx ++
      // this.dataInit = this.dataInit.concat(data1)
      // this.data = this.isOddEvent(adpater(this.dataInit))
      const _n = this.isOddEvent(adpater(adapterData(data1)))
      // this.data.left = this.data.left.concat(_n.left)
      // this.data.right = this.data.right.concat(_n.right)
      this.$left.append(_n.left)
      this.$right.append(_n.right)
      this.dataInit = this.dataInit.concat(adapterData(data1))
      // this.$other.update({title: '玩命加载中，请稍等...', itemClass: 'waterfall-load-tips'}, ()=> {
        
      // })
      // that.updateData()
    }
    else {
      this.$other.update({title: '人家也是有底线的~', itemClass: 'waterfall-load-tips'})
    }
    
  },
  updateData(type) {
    // this.$left.update(this.data.left)
    // this.$right.update(this.data.right)
    this.setData({
      'waterfallLeft.data': this.data.left,
      'waterfallRight.data': this.data.right
    })
  },
  onLoad() {
    this.dataInit = adapterData(data1)
    this.data = this.isOddEvent(adpater(this.dataInit))
  }
})